package com.interview.controller;


import com.interview.pojo.Article;
import com.interview.pojo.Solr_Article;
import com.interview.service.ArticleService;
import com.interview.service.utils.Springboot_SolrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/solr")
public class SolrController {

    @Autowired
   private Springboot_SolrUtils server;

    @Autowired
    private ArticleService articleService;

    // http://localhost:10087/solr/soleHighlighting
    @GetMapping("/soleHighlighting")
    public   List<Map<String, String>> soleHighlighting(String title){
        //需要显示的字段   id字段默认添加
        String[] fields={"head","url_params"};
        List<Map<String, String>> list = server.soleHighlighting("head", title, 0, 50, "time", fields);
        return list;
    }


    //录入全部数据
    // http://localhost:10087/solr/soleAdd
    @GetMapping("/soleAdd")
    public  void  soleAdd(){

        List<Article> list = articleService.queryAll();
        System.out.println(list.size());
        for (Article article : list) {
            //添加到搜索里
            Solr_Article solr_article=new Solr_Article();
            solr_article.setId(article.getId());
            solr_article.setHead(article.getHead());
            solr_article.setType(article.getCatalogue_id());
            solr_article.setTime(article.getTime());
            solr_article.setUrl_params("catalogueId="+article.getId());
            System.out.println(solr_article);
            server.soleAdd(solr_article);
        }

    }

    //删除全部数据
    // http://localhost:10087/solr/deleteDocumentAll
    @GetMapping("/deleteDocumentAll")
    public   String deleteDocumentAll(){
        try {
            server.deleteDocumentAll();

        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
        return "true";
    }



}
